package com.icss.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.icss.common.service.BaseServiceImpl;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.common.vo.JSONEnum;
import com.icss.dao.IMakeupOrdersDao;
import com.icss.dao.IMakeupOrdersProdDao;
import com.icss.dao.impl.MakeupOrdersDaoImpl;
import com.icss.dao.impl.MakeupOrdersProdDaoImpl;
import com.icss.service.IOrdersDbService;
import com.icss.service.IProductDbService;
import com.icss.service.IUsersDbService;
import com.icss.vo.MakeupCart;
import com.icss.vo.MakeupOrders;
import com.icss.vo.MakeupOrdersProd;
import com.icss.vo.MakeupProduct;
import com.icss.vo.MakeupUsers;

/**
 * Service实现类(订单代码)
 * 
 * @author 陈岩
 */
public class OrdersDbServiceImpl extends BaseServiceImpl implements IOrdersDbService
{
	private IMakeupOrdersDao ordersDao = new MakeupOrdersDaoImpl();
	private IMakeupOrdersProdDao ordersProductDao = new MakeupOrdersProdDaoImpl();
	
	private IUsersDbService usersDbService;
	private IProductDbService productDbService;
	
	@Override
	public ApiResponse<Object> saveOneOrdersService(MakeupOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.save(orders);
		if (res > 0)
		{
			double totalPrice = 0 ; 
			/* 存储订单明细 */
			for (Iterator iterator = orders.getCartList().iterator(); iterator.hasNext();)
			{
				MakeupCart cart = (MakeupCart) iterator.next();
				MakeupProduct product = cart.getProduct(); 
				
				MakeupOrdersProd ordersProduct = new MakeupOrdersProd();
				ordersProduct.setOrdersId(orders.getId());
				ordersProduct.setProdId(cart.getProdId());
				
				ordersProduct.setName(product.getName());
				ordersProduct.setPrice(product.getPrice());
				ordersProduct.setNum(cart.getNum());
				ordersProduct.setTotalPrice(ordersProduct.getPrice() * ordersProduct.getNum());
				
				/* 总价格相加 */
				totalPrice = totalPrice + ordersProduct.getTotalPrice() ; 
				/* 订单商品 */
				this.saveOneOrdersProductService(ordersProduct);
				
				condMap.clear();
				condMap.put("id", cart.getId());
				/* 清空购物车 */
				this.usersDbService.deleteOneCartService(condMap);
				
				/* 更新商品的销量 */
				product.setSaleNum(product.getSaleNum() + ordersProduct.getNum());
				product.setUpdateTime(new Date());
				this.productDbService.updateOneProductService(product);
			}
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersService(MakeupOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.update(orders);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupOrders> findCondListOrdersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<MakeupOrders> apiResponse = new ApiResponse<MakeupOrders>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<MakeupOrders> demoOrdersList = Collections.EMPTY_LIST;

		Map<String, List<MakeupOrders>> dataList = new HashMap<String, List<MakeupOrders>>();
		demoOrdersList = this.ordersDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersList.iterator(); iterator.hasNext();)
			{
				MakeupOrders orders = (MakeupOrders) iterator.next();
				
				extMap.clear();
				extMap.put("id", orders.getUsersId());
				ApiResponse<MakeupUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
				MakeupUsers users = cateApiResponse.getDataOneJava();
				orders.setUsers(users);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupOrders> findOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<MakeupOrders> apiResponse = new ApiResponse<MakeupOrders>();

		Map<String, MakeupOrders> data = new HashMap<String, MakeupOrders>();
		MakeupOrders orders = this.ordersDao.findOne(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", orders.getUsersId());
			ApiResponse<MakeupUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
			MakeupUsers users = cateApiResponse.getDataOneJava();
			orders.setUsers(users);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), orders);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(orders);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneOrdersProductService(MakeupOrdersProd ordersProduct)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		ordersProduct.setCreateTime(new Date());
		int res = this.ordersProductDao.save(ordersProduct);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersProduct.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersProduct);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersProductService(MakeupOrdersProd ordersProduct)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersProductDao.update(ordersProduct);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersProduct.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersProduct);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersProductService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersProductDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupOrdersProd> findCondListOrdersProductService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<MakeupOrdersProd> apiResponse = new ApiResponse<MakeupOrdersProd>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<MakeupOrdersProd> demoOrdersProductList = Collections.EMPTY_LIST;

		Map<String, List<MakeupOrdersProd>> dataList = new HashMap<String, List<MakeupOrdersProd>>();
		demoOrdersProductList = this.ordersProductDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersProductList.iterator(); iterator.hasNext();)
			{
				MakeupOrdersProd ordersProduct = (MakeupOrdersProd) iterator.next();
				
				extMap.clear();
				extMap.put("id", ordersProduct.getOrdersId());
				ApiResponse<MakeupOrders> ordersApiResponse = this.findOneOrdersService(extMap);
				MakeupOrders orders = ordersApiResponse.getDataOneJava();
				ordersProduct.setOrders(orders);
				
				extMap.clear();
				extMap.put("id", ordersProduct.getProdId());
				ApiResponse<MakeupProduct> productApiResponse = this.productDbService.findOneProductService(extMap);
				MakeupProduct product = productApiResponse.getDataOneJava();
				ordersProduct.setProduct(product);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersProductList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersProductList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<MakeupOrdersProd> findOneOrdersProductService(Map<String, Object> condMap)
	{
		ApiResponse<MakeupOrdersProd> apiResponse = new ApiResponse<MakeupOrdersProd>();

		Map<String, MakeupOrdersProd> data = new HashMap<String, MakeupOrdersProd>();
		MakeupOrdersProd ordersProduct = this.ordersProductDao.findOne(condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", ordersProduct.getOrdersId());
			ApiResponse<MakeupOrders> ordersApiResponse = this.findOneOrdersService(extMap);
			MakeupOrders orders = ordersApiResponse.getDataOneJava();
			ordersProduct.setOrders(orders);
			
			extMap.clear();
			extMap.put("id", ordersProduct.getProdId());
			ApiResponse<MakeupProduct> productApiResponse = this.productDbService.findOneProductService(extMap);
			MakeupProduct product = productApiResponse.getDataOneJava();
			ordersProduct.setProduct(product);
		}
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersProduct);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersProduct);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	public void setUsersDbService(IUsersDbService usersDbService)
	{
		this.usersDbService = usersDbService;
	}

	public void setProductDbService(IProductDbService productDbService)
	{
		this.productDbService = productDbService;
	}
}
